package com.jy.rhin.application.cda;

import com.jy.api.hospital.HospitalAPI;
import com.jy.api.hospital.dto.HospitalDTO;
import com.jy.api.misc.dto.DaiMaDto;
import com.jy.api.misc.dto.ZhiYuMXCXDto;
import com.jy.api.patient.dto.BingRenXXDto;
import com.jy.common.StringUtil;
import com.jy.framework.base.utils.Converter;
import com.jy.rhin.application.cda.enums.HealthDictionaryEnum;
import com.jy.rhin.domain.cda.CDACreator;
import com.jy.rhin.infra.cda.CDAException;
import com.jy.rhin.infra.cda.feign.PatientService;
import com.jy.rhin.infra.cda.feign.ShuYuService;
import com.jy.rhin.infra.cda.service.TangNiaoBSFService;
import com.jy.rhin.infra.cda.tnb.TangNiaoBSFJCVo;
import com.jy.rhin.infra.cda.tnb.TangNiaoBSFVo;
import com.jy.rhin.infra.cda.tnb.TangNiaoBSFYYVo;
import com.jy.rhin.infra.cda.util.ZhiJiUtils;
import com.jy.rhin.model.ClinicalDocumentData;
import com.jy.rhin.model.CodeObject;
import com.jy.rhin.model.TangNiaoBSF;
import com.jy.rhin.model.ValueUnit;
import com.jy.rhin.model.body.gaoxueysf.*;
import com.jy.rhin.model.body.tangniaobsf.ShengHuoFS;
import com.jy.rhin.model.body.tangniaobsf.ShengMingTZ;
import com.jy.rhin.model.body.tangniaobsf.ShiYanSJC;
import com.jy.rhin.model.body.tangniaobsf.YongYao;
import com.jy.rhin.model.body.tangniaobsf.ZhiLiaoJH;
import com.jy.rhin.model.body.tangniaobsf.*;
import com.jy.rhin.model.header.*;
import com.jy.rhin.utils.CDAUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

@Service
public class TangNiaoBSFApplication {
    @Autowired
    private CDACreator cdaCreator;
    @Autowired
    private TangNiaoBSFService tangNiaoBSFService;
    @Autowired
    private PatientService patientService;
   /* @Autowired
    private DataMigrationAPI dataMigrationAPI;*/
    @Autowired
    private ShuYuService shuYuService;
    @Resource
    private HospitalAPI hospitalAPI;

    public String create(String tangNiaoBSFID, String yeWuJGDM) {
        return cdaCreator.create(createTangNiaoBSF(tangNiaoBSFID, yeWuJGDM), "13tangniaobingsf.ftl");
    }

    public ClinicalDocumentData createTangNiaoBSF(String tangNiaoBSFID, String yeWuJGDM) {
        TangNiaoBSFVo tangNiaoBSFVo = tangNiaoBSFService.get(tangNiaoBSFID, yeWuJGDM);
        if (Objects.isNull(tangNiaoBSFVo)) {
            throw new CDAException("主业务数据不存在", null);
        }
        BingRenXXDto bingRenInfo = patientService.queryPatientById(tangNiaoBSFVo.getPATIENTID());
        if (Objects.isNull(bingRenInfo)) {
            throw new CDAException("患者信息不存在", null);
        }
        HospitalDTO hospital = hospitalAPI.info(yeWuJGDM).getData();
//        HospitalDTO hospital = dataMigrationAPI.getByHospitalId(yeWuJGDM).getData();
        if (Objects.isNull(hospital)) {
            throw new CDAException("机构数据不存在", null);
        }

        List<TangNiaoBSFYYVo> tangNiaoBSFYYs = tangNiaoBSFService.getYongYao(tangNiaoBSFID, yeWuJGDM);

        List<TangNiaoBSFJCVo> tangNiaoBSFJCs = tangNiaoBSFService.getJianCha(tangNiaoBSFID, yeWuJGDM);

        shuYuZhiYuQuery(bingRenInfo, tangNiaoBSFVo, tangNiaoBSFYYs);

        TangNiaoBSF docData = new TangNiaoBSF();
        docData.setId(tangNiaoBSFID);
        setClinicalDocumentData(docData, tangNiaoBSFVo, hospital, bingRenInfo);

        SuiFangSJ suiFangSJ = new SuiFangSJ();
        suiFangSJ.setBenCiSFSJ(CDAUtil.toD8(tangNiaoBSFVo.getSUIFANGRQ()));
        suiFangSJ.setSuiFangFS(new CodeObject(tangNiaoBSFVo.getSUIFANGFS(), tangNiaoBSFVo.getSUIFANGFSMC()));
        docData.setSuiFangSJ(suiFangSJ);

        ArrayList<ZhengZhuang> zhengZhuangs = new ArrayList<>();
        String[] zhengZhuangDMs = tangNiaoBSFVo.getZHENGZHUANG().split("\\|");
        List<String> zhengZhuangDM2s = new ArrayList<>();
        List<String> zhengZhuangMCs = new ArrayList<>();
        for (int i = 0; i < zhengZhuangDMs.length; i++) {
            if ("1".equals(zhengZhuangDMs[i])) {
                zhengZhuangDM2s.add("R42.x00");
                zhengZhuangMCs.add("头晕和眩晕");
                zhengZhuangDM2s.add("R51.x00");
                zhengZhuangMCs.add("头痛");
            } else if ("2".equals(zhengZhuangDMs[i])) {
                zhengZhuangDM2s.add("R11.x00");
                zhengZhuangMCs.add("恶心和呕吐");
            } else if ("3".equals(zhengZhuangDMs[i])) {
                zhengZhuangDM2s.add("H93.100");
                zhengZhuangMCs.add("耳鸣");
            } else if ("4".equals(zhengZhuangDMs[i])) {
                zhengZhuangDM2s.add("R06.000");
                zhengZhuangMCs.add("呼吸困难");
            } else if ("5".equals(zhengZhuangDMs[i])) {
                zhengZhuangDM2s.add("R00.200");
                zhengZhuangMCs.add("心悸");
                zhengZhuangDM2s.add("R09.800");
                zhengZhuangMCs.add("胸闷");
            } else if ("6".equals(zhengZhuangDMs[i])) {
                zhengZhuangDM2s.add("R04.000");
                zhengZhuangMCs.add("鼻出血");
            } else if ("7".equals(zhengZhuangDMs[i])) {
                zhengZhuangDM2s.add("R20.803");
                zhengZhuangMCs.add("肢体麻木");
            } else if ("8".equals(zhengZhuangDMs[i])) {
                zhengZhuangDM2s.add("R60.001");
                zhengZhuangMCs.add("下肢水肿");
            }
        }
        if (zhengZhuangDM2s.size() == 0) {
            zhengZhuangDM2s.add("Z00.001");
            zhengZhuangMCs.add("健康查体");
        }
        for (int i = 0; i < zhengZhuangDM2s.size(); i++) {
            ZhengZhuang zhengZhuang = new ZhengZhuang();
            zhengZhuang.setZhengZhuangDM(new CodeObject(zhengZhuangDM2s.get(i), zhengZhuangMCs.get(i)));
            zhengZhuang.setZhengZhuangMC(zhengZhuangMCs.get(i));
            zhengZhuangs.add(zhengZhuang);
        }
        docData.setZhengZhuang(zhengZhuangs);

        ShengMingTZ shengMingTZ = new ShengMingTZ();
        shengMingTZ.setTiZhong(Converter.toString(tangNiaoBSFVo.getTIZHONG().setScale(1, BigDecimal.ROUND_HALF_UP)));
        shengMingTZ.setZuBeiDMBDBZ(Arrays.asList("1", "2", "3").contains(Converter.toString(tangNiaoBSFVo.getZUBEIDMBD(), "0")) ? "true" : "false");
        shengMingTZ.setShouSuoYa(Converter.toString(tangNiaoBSFVo.getSHOUSUOY()));
        shengMingTZ.setShuZhangYa(Converter.toString(tangNiaoBSFVo.getSHUZHANGY()));
        shengMingTZ.setTiZhiZS(Converter.toString(tangNiaoBSFVo.getBMIZS().setScale(2, BigDecimal.ROUND_HALF_UP)));
        shengMingTZ.setQiTaYXTZ(!StringUtil.isNullOrEmpty(tangNiaoBSFVo.getQITATZ()) ? tangNiaoBSFVo.getQITATZ() : "-");
        docData.setShengMingTZ(shengMingTZ);

        ShengHuoFS shengHuoFS = new ShengHuoFS();
        shengHuoFS.setRiXiYL(Converter.toString(tangNiaoBSFVo.getXIYANL()));
        shengHuoFS.setRiYinJL(Converter.toString(tangNiaoBSFVo.getYINJIUL()));
        if (tangNiaoBSFVo.getMEIZHOUYDCS() == 0) {
            shengHuoFS.setDuanLianPL(new CodeObject("4", "不运动"));
        } else if (tangNiaoBSFVo.getMEIZHOUYDCS() <= 2) {
            shengHuoFS.setDuanLianPL(new CodeObject("23", "1次/周～2次/周"));
        } else if (tangNiaoBSFVo.getMEIZHOUYDCS() <= 4) {
            shengHuoFS.setDuanLianPL(new CodeObject("22", "3次/周～4次/周"));
        } else if (tangNiaoBSFVo.getMEIZHOUYDCS() <= 6) {
            shengHuoFS.setDuanLianPL(new CodeObject("21", "5次/周～6次/周"));
        } else {
            shengHuoFS.setDuanLianPL(new CodeObject("2", "每周一次以上"));
        }
        shengHuoFS.setMeiCiDLSJ(Converter.toString(tangNiaoBSFVo.getMEICIYDSJ()));
        shengHuoFS.setRiZhuSL(Converter.toString(tangNiaoBSFVo.getZHUSHIL()));
        shengHuoFS.setXinLiTZ(new CodeObject(tangNiaoBSFVo.getXINLITZ(), tangNiaoBSFVo.getXINLITZMC()));
        shengHuoFS.setZunYiXW(new CodeObject(tangNiaoBSFVo.getZUNYIXW(), tangNiaoBSFVo.getZUNYIXWMC()));
        docData.setShengHuoFS(shengHuoFS);

        ZhiLiaoJH zhiLiaoJH = new ZhiLiaoJH();
        zhiLiaoJH.setMuBiaoTZ(Converter.toString(tangNiaoBSFVo.getMUBIAOTZ().setScale(1, BigDecimal.ROUND_HALF_UP)));
        zhiLiaoJH.setMuBiaoTZZS(Converter.toString(tangNiaoBSFVo.getMUBIAOBMIZS().setScale(2, BigDecimal.ROUND_HALF_UP)));
        zhiLiaoJH.setMuBiaoRXYL(Converter.toString(tangNiaoBSFVo.getMUBIAOXYL().setScale(0, BigDecimal.ROUND_HALF_UP)));
        zhiLiaoJH.setMuBiaoRYJL(Converter.toString(tangNiaoBSFVo.getMUBIAOYJL().setScale(0, BigDecimal.ROUND_HALF_UP)));
        zhiLiaoJH.setMuBiaoYDSC(Converter.toString(tangNiaoBSFVo.getMUBIAOMCYDSJ()));
        if (tangNiaoBSFVo.getMUBIAOMZYDCS() == 0) {
            zhiLiaoJH.setMuBiaoDLPL(new CodeObject("4", "不运动"));
        } else if (tangNiaoBSFVo.getMUBIAOMZYDCS() <= 2) {
            zhiLiaoJH.setMuBiaoDLPL(new CodeObject("23", "1次/周～2次/周"));
        } else if (tangNiaoBSFVo.getMUBIAOMZYDCS() <= 4) {
            zhiLiaoJH.setMuBiaoDLPL(new CodeObject("22", "3次/周～4次/周"));
        } else if (tangNiaoBSFVo.getMUBIAOMZYDCS() <= 6) {
            zhiLiaoJH.setMuBiaoDLPL(new CodeObject("21", "5次/周～6次/周"));
        } else {
            zhiLiaoJH.setMuBiaoDLPL(new CodeObject("2", "每周一次以上"));
        }
        zhiLiaoJH.setMuBiaoRZSL(Converter.toString(tangNiaoBSFVo.getJIANYIZSL()));
        docData.setZhiLiaoJH(zhiLiaoJH);

        ShiYanSJC shiYanSJC = new ShiYanSJC();
        shiYanSJC.setKongFuXT(Converter.toString(tangNiaoBSFVo.getKONGFUXT().setScale(1, BigDecimal.ROUND_HALF_UP), "0.0"));
        shiYanSJC.setTangHuaXHDB(Converter.toString(tangNiaoBSFVo.getTANGHUAXHDB(), "4.0"));
        List<ShiYanSJCXM> shiYanSJCXMs = new ArrayList<>();
        if (!CollectionUtils.isEmpty(tangNiaoBSFJCs)) {
            for (TangNiaoBSFJCVo infoVo : tangNiaoBSFJCs) {
                ShiYanSJCXM shiYanSJCXM = new ShiYanSJCXM();
                shiYanSJCXM.setJianCeRQ(CDAUtil.toD8(infoVo.getJIANCHARQ()));
                shiYanSJCXM.setFuZhuJCXM(infoVo.getFUZHUJCXM());
                shiYanSJCXM.setFuZhuJCRYXM(infoVo.getJIANCHARYXM());
                shiYanSJCXM.setFuZhuJCJG(infoVo.getFUZHUJCJG());
                shiYanSJCXMs.add(shiYanSJCXM);
            }
        } else {
            ShiYanSJCXM shiYanSJCXM = new ShiYanSJCXM();
            shiYanSJCXM.setJianCeRQ("20231002");
            shiYanSJCXM.setFuZhuJCXM("糖源检测");
            shiYanSJCXM.setFuZhuJCRYXM("周佳");
            shiYanSJCXM.setFuZhuJCJG("异常");
            shiYanSJCXMs.add(shiYanSJCXM);
        }
        shiYanSJC.setShiYanSJCXM(shiYanSJCXMs);
        docData.setShiYanSJC(shiYanSJC);

        List<YongYao> yongYaoList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(tangNiaoBSFYYs)) {
            for (TangNiaoBSFYYVo infoVo : tangNiaoBSFYYs) {
                YongYao yongYao = new YongYao();
                if ("1".equals(infoVo.getYIDAOSBZ())) {
                    continue;
                }
                if (infoVo.getZHONGYAOLBDM() != null) {
                    yongYao.setZhongYaoSYLB(new CodeObject(Converter.toString(infoVo.getZHONGYAOLBDM()), infoVo.getZHONGYAOLBMC()));
                }
                YongYaoItem yongYaoItem = new YongYaoItem();
                if (!StringUtil.isNullOrEmpty(infoVo.getGEIYAOTJ())) {
                    yongYaoItem.setYongYaoTJ(new CodeObject(Converter.toString(infoVo.getGEIYAOTJ()), infoVo.getGEIYAOTJMC()));
                } else {
                    yongYaoItem.setYongYaoTJ(new CodeObject("9", "其他用药途径"));
                }
                yongYaoItem.setDanCiYYJL(new ValueUnit(Converter.toString(new BigDecimal(getNum(infoVo.getYAOWUSYCJL())).setScale(2, BigDecimal.ROUND_HALF_UP)),
                        Converter.toString(infoVo.getJILIANGDW(), infoVo.getYAOWUSYCJL().replace(getNum(infoVo.getYAOWUSYCJL()), ""))));
                yongYaoItem.setYaoWuSYPL(new ValueUnit(getNum(infoVo.getYAOWUSYPL()), "次/日"));
                yongYaoItem.setYaoPinMC(infoVo.getYAOPINMC());
                yongYaoItem.setFuYaoYCX(new CodeObject(tangNiaoBSFVo.getFUYAOYCX(), tangNiaoBSFVo.getFUYAOYCXMC().replace("规律服药", "规律")));
                yongYaoItem.setYongYaoFZY(!StringUtil.isNullOrEmpty(tangNiaoBSFVo.getYAOWUBLFYXS()) ? tangNiaoBSFVo.getYAOWUBLFYXS() : "-");
                yongYaoItem.setYongYaoFZYBZ(tangNiaoBSFVo.getYAOWUBLFY() == 1 ? "true" : "false");
                yongYao.getYongYaoItemList().add(yongYaoItem);

                if (yongYao.getZhongYaoSYLB() == null) {
                    yongYao.setZhongYaoSYLB(new CodeObject("1", "未使用"));
                }

                if ("1".equals(infoVo.getYIDAOSBZ())) {
                    yongYao.setYiDaoSDCJL(new ValueUnit(Converter.toString(new BigDecimal(getNum(infoVo.getYAOWUSYCJL())).setScale(0, BigDecimal.ROUND_HALF_UP)),
                            Converter.toString(infoVo.getJILIANGDW(), infoVo.getYAOWUSYCJL().replace(getNum(infoVo.getYAOWUSYCJL()), ""))));
                    yongYao.setYiDaoSSYPL(new ValueUnit(getNum(infoVo.getYAOWUSYPL()), "次/日"));
                    yongYao.setYiDaoSMC(infoVo.getYAOPINMC());
                } else {
                    yongYao.setYiDaoSDCJL(new ValueUnit("0", "支"));
                    yongYao.setYiDaoSSYPL(new ValueUnit("0", "次/日"));
                    yongYao.setYiDaoSMC("-");
                }
                yongYao.setDiXueTFY(new CodeObject(Converter.toString(Converter.toInt(tangNiaoBSFVo.getDIXUETFY()) + 1), tangNiaoBSFVo.getDIXUETFYMC()));
            }
        }
        docData.setYongYao(yongYaoList);

        JianKangPG jianKangPG = new JianKangPG();
        jianKangPG.setSuiFangPJ(new CodeObject(tangNiaoBSFVo.getCICISFFL(), tangNiaoBSFVo.getCICISFFLMC()));
        docData.setJianKangPG(jianKangPG);

        ZhuanZhenJY zhuanZhenJY = new ZhuanZhenJY();
        zhuanZhenJY.setZhuanZhenBZ(tangNiaoBSFVo.getSHIFOUZZ() == 1 ? "true" : "false");
        zhuanZhenJY.setZhuanZhenYY(!StringUtil.isNullOrEmpty(tangNiaoBSFVo.getZHUANZHENYYZD()) ? tangNiaoBSFVo.getZHUANZHENYYZD() : "-");
        zhuanZhenJY.setZhuanRuJGMC(!StringUtil.isNullOrEmpty(tangNiaoBSFVo.getZHUANZHENYYJKB()) ? tangNiaoBSFVo.getZHUANZHENYYJKB() : "-");
        zhuanZhenJY.setZhuanRuKSMC(!StringUtil.isNullOrEmpty(tangNiaoBSFVo.getZHUANZHENYYJKB()) ? tangNiaoBSFVo.getZHUANZHENYYJKB() : "-");
        docData.setZhuanZhenJY(zhuanZhenJY);

        XiaCiSFAP xiaCiSFAP = new XiaCiSFAP();
        xiaCiSFAP.setXiaCiSFRQ(CDAUtil.toD8(tangNiaoBSFVo.getXIACISFRQ()));
        docData.setXiaCiSFAP(xiaCiSFAP);

        setDefaultDocData(docData);
        return docData;
    }

    private String getNum(String value) {
        try {
            String reg = "\\d+\\.?\\d*";
            Pattern pattern = Pattern.compile(reg);
            Matcher matcher = pattern.matcher(value.replaceAll(" ", ""));
            boolean b = matcher.find();
            if (!b) {
                return "1";
            }
            String group = matcher.group();
            return group;
        } catch (Exception ex) {
            return "1";
        }
    }

    private void shuYuZhiYuQuery(BingRenXXDto bingRenInfo, TangNiaoBSFVo tangNiaoBSFVo, List<TangNiaoBSFYYVo> tangNiaoBSFYYs) {
        List<ZhiYuMXCXDto> zhiJiBSFList = new ArrayList<>();
        ZhiJiUtils.addValueItem(zhiJiBSFList, HealthDictionaryEnum.GB_T_2261_1.getCode(), new HashSet<String>(Arrays.asList(bingRenInfo.getXingBieDM())));

        ZhiJiUtils.addValueItem(zhiJiBSFList, HealthDictionaryEnum.CV06_00_207.getCode(), new HashSet<String>(Arrays.asList(Converter.toString(tangNiaoBSFVo.getSUIFANGFS()))));
        ZhiJiUtils.addValueItem(zhiJiBSFList, HealthDictionaryEnum.CVX_XLTZ.getCode(), new HashSet<String>(Arrays.asList(Converter.toString(tangNiaoBSFVo.getXINLITZ()))));
        ZhiJiUtils.addValueItem(zhiJiBSFList, HealthDictionaryEnum.CVX_ZYXW.getCode(), new HashSet<String>(Arrays.asList(Converter.toString(tangNiaoBSFVo.getZUNYIXW()))));
        if (!CollectionUtils.isEmpty(tangNiaoBSFYYs)) {
            ZhiJiUtils.addValueItem(zhiJiBSFList, HealthDictionaryEnum.CV06_00_101.getCode(), tangNiaoBSFYYs.stream().filter(o -> o.getZHONGYAOLBDM() != null).map(o -> Converter.toString(o.getZHONGYAOLBDM())).collect(Collectors.toSet()));
            ZhiJiUtils.addValueItem(zhiJiBSFList, HealthDictionaryEnum.CV06_00_102.getCode(), tangNiaoBSFYYs.stream().filter(o -> !StringUtil.isNullOrEmpty(o.getGEIYAOTJ())).map(o -> Converter.toString(o.getGEIYAOTJ())).collect(Collectors.toSet()));
        }
        ZhiJiUtils.addValueItem(zhiJiBSFList, HealthDictionaryEnum.CVX_FYYCX.getCode(), new HashSet<String>(Arrays.asList(Converter.toString(tangNiaoBSFVo.getFUYAOYCX()))));
        ZhiJiUtils.addValueItem(zhiJiBSFList, HealthDictionaryEnum.CV05_10_012.getCode(), new HashSet<String>(Arrays.asList(Converter.toString(tangNiaoBSFVo.getCICISFFL()))));
        ZhiJiUtils.addValueItem(zhiJiBSFList, HealthDictionaryEnum.CVX_DXTFY.getCode(), new HashSet<String>(Arrays.asList(Converter.toString(Converter.toInt(tangNiaoBSFVo.getDIXUETFY()) + 1))));

        Map<String, List<DaiMaDto>> valueByCode = shuYuService.getValueByCode(zhiJiBSFList.toArray(new ZhiYuMXCXDto[0]));
        bingRenInfo.setXingBie(ZhiJiUtils.getValue(HealthDictionaryEnum.GB_T_2261_1.getCode(), Converter.toString(bingRenInfo.getXingBieDM()), valueByCode));

        tangNiaoBSFVo.setSUIFANGFSMC(ZhiJiUtils.getValue(HealthDictionaryEnum.CV06_00_207.getCode(), Converter.toString(tangNiaoBSFVo.getSUIFANGFS()), valueByCode));
        tangNiaoBSFVo.setXINLITZMC(ZhiJiUtils.getValue(HealthDictionaryEnum.CVX_XLTZ.getCode(), Converter.toString(tangNiaoBSFVo.getXINLITZ()), valueByCode));
        tangNiaoBSFVo.setZUNYIXWMC(ZhiJiUtils.getValue(HealthDictionaryEnum.CVX_ZYXW.getCode(), Converter.toString(tangNiaoBSFVo.getZUNYIXW()), valueByCode));
        if (!CollectionUtils.isEmpty(tangNiaoBSFYYs)) {
            tangNiaoBSFYYs.forEach(info -> {
                info.setZHONGYAOLBMC(ZhiJiUtils.getValue(HealthDictionaryEnum.CV06_00_101.getCode(), Converter.toString(info.getZHONGYAOLBDM()), valueByCode));
                info.setGEIYAOTJMC(ZhiJiUtils.getValue(HealthDictionaryEnum.CV06_00_102.getCode(), Converter.toString(info.getGEIYAOTJ()), valueByCode));
            });
        }
        tangNiaoBSFVo.setFUYAOYCXMC(ZhiJiUtils.getValue(HealthDictionaryEnum.CVX_FYYCX.getCode(), Converter.toString(tangNiaoBSFVo.getFUYAOYCX()), valueByCode));
        tangNiaoBSFVo.setCICISFFLMC(ZhiJiUtils.getValue(HealthDictionaryEnum.CV05_10_012.getCode(), Converter.toString(tangNiaoBSFVo.getCICISFFL()), valueByCode));
        tangNiaoBSFVo.setDIXUETFYMC(ZhiJiUtils.getValue(HealthDictionaryEnum.CVX_DXTFY.getCode(), Converter.toString(Converter.toInt(tangNiaoBSFVo.getDIXUETFY()) + 1), valueByCode));

    }

    TangNiaoBSF setClinicalDocumentData(TangNiaoBSF clinicalDocumentData, TangNiaoBSFVo tangNiaoBSFVo, HospitalDTO hospital, BingRenXXDto bingRenInfo) {
        clinicalDocumentData.setEffectiveTime(CDAUtil.toD15(tangNiaoBSFVo.getLURURQ()));
        clinicalDocumentData.setPatientRole(createPatientRole(tangNiaoBSFVo, bingRenInfo));
        clinicalDocumentData.setAuthor(createAuthor(tangNiaoBSFVo, hospital));
        clinicalDocumentData.setCustodian(createCustodian(hospital));
        return clinicalDocumentData;
    }

    Author createAuthor(TangNiaoBSFVo tangNiaoBSFVo, HospitalDTO hospital) {
        Author author = new Author();
        author.setTime(CDAUtil.toD15(tangNiaoBSFVo.getSUIFANGRQ()));
        author.setId(!StringUtil.isNullOrEmpty(tangNiaoBSFVo.getSUIFANGYSBM()) ? tangNiaoBSFVo.getSUIFANGYSBM() : tangNiaoBSFVo.getSUIFANGYSXM());
        author.setName(tangNiaoBSFVo.getSUIFANGYSXM());
        author.setOrganizationId(CDAUtil.toJGID10(hospital.getOutId()));
        author.setOrganizationName(hospital.getName());
        author.setOrganizationTelecom(hospital.getDanWeiDH());
        author.setOrganizationAddr(hospital.getAddress());
        return author;
    }

    Custodian createCustodian(HospitalDTO hospital) {
        Custodian custodian = new Custodian();
        custodian.setId(CDAUtil.toJGID10(hospital.getOutId()));
        custodian.setName(hospital.getName());
        custodian.setTelecom(hospital.getDanWeiDH());
        custodian.setAddr(hospital.getAddress());
        return custodian;
    }

    PatientRole createPatientRole(TangNiaoBSFVo tangNiaoBSFVo, BingRenXXDto bingRenInfo) {
        PatientRole patientRole = new PatientRole();
        patientRole.setId(CDAUtil.formatNumber(tangNiaoBSFVo.getGERENDABS()));
        patientRole.setTelecom(bingRenInfo.getShouJiHao());
        patientRole.setAddr(createAddr(bingRenInfo));
        patientRole.setPatient(createPatient(bingRenInfo));
        return patientRole;
    }

    Addr createAddr(BingRenXXDto bingRenXXDto) {
        Addr addr = new Addr();
        addr.setState(bingRenXXDto.getJuZhuDSFMC());
        addr.setCity(bingRenXXDto.getJuZhuDSMC());
        addr.setCounty(bingRenXXDto.getJuZhuDQMC());
        addr.setTownship(bingRenXXDto.getJuZhuDJDMC());
        addr.setStreetName(bingRenXXDto.getJuZhuDSQMC());
        addr.setHouseNumber(bingRenXXDto.getJuZhuDMPH());
        addr.setPostalCode(bingRenXXDto.getLianXiRYB());
        return addr;
    }

    Patient createPatient(BingRenXXDto bingRenXXDto) {
        Patient patient = new Patient();
        patient.setId(bingRenXXDto.getZhengJianHM());
        patient.setName(bingRenXXDto.getBingRenXM());
        patient.setAdministrativeGender(new CodeObject(bingRenXXDto.getXingBieDM(), bingRenXXDto.getXingBie()));
        patient.setBirthTime(CDAUtil.toD8(Converter.toDate(bingRenXXDto.getChuShengRQ())));
        patient.setMaritalStatus(new CodeObject(bingRenXXDto.getHunYinZT(), bingRenXXDto.getHunYinZTMC()));
        if (!StringUtil.isNullOrEmpty(bingRenXXDto.getMinZuDM()) && !StringUtil.isNullOrEmpty(bingRenXXDto.getMinZuMC())) {
            patient.setEthnicGroup(new CodeObject(bingRenXXDto.getMinZuDM(), bingRenXXDto.getMinZuMC()));
        }
        return patient;
    }

    private void setDefaultDocData(TangNiaoBSF docData) {
        if (docData.getSuiFangSJ() == null || docData.getSuiFangSJ().getBenCiSFSJ() == null) {
            SuiFangSJ suiFangSJ = new SuiFangSJ();
            suiFangSJ.setBenCiSFSJ(CDAUtil.defaultD8());
            suiFangSJ.setSuiFangFS(new CodeObject("2", "家庭"));
            docData.setSuiFangSJ(suiFangSJ);
        }
        if (CollectionUtils.isEmpty(docData.getZhengZhuang())) {
            List<ZhengZhuang> zhengZhuangs = new ArrayList<>();
            ZhengZhuang zhengZhuang = new ZhengZhuang();
            zhengZhuang.setZhengZhuangDM(new CodeObject("Z00.001", "健康查体"));
            zhengZhuang.setZhengZhuangMC("健康查体");
            zhengZhuangs.add(zhengZhuang);
            docData.setZhengZhuang(zhengZhuangs);
        }
        if (docData.getShengMingTZ() == null) {
            ShengMingTZ shengMingTZ = new ShengMingTZ();
            shengMingTZ.setTiZhong("66.0");
            shengMingTZ.setShouSuoYa("177");
            shengMingTZ.setShuZhangYa("101");
            shengMingTZ.setTiZhiZS("24.24");
            shengMingTZ.setQiTaYXTZ("无");
            shengMingTZ.setZuBeiDMBDBZ("false");
            docData.setShengMingTZ(shengMingTZ);
        } else {
            if (StringUtil.isNullOrEmpty(docData.getShengMingTZ().getTiZhong())) {
                docData.getShengMingTZ().setTiZhong("66.0");
            }
            if (StringUtil.isNullOrEmpty(docData.getShengMingTZ().getZuBeiDMBDBZ())) {
                docData.getShengMingTZ().setZuBeiDMBDBZ("false");
            }
            if (StringUtil.isNullOrEmpty(docData.getShengMingTZ().getShouSuoYa())) {
                docData.getShengMingTZ().setShouSuoYa("177");
            }
            if (StringUtil.isNullOrEmpty(docData.getShengMingTZ().getShuZhangYa())) {
                docData.getShengMingTZ().setShuZhangYa("101");
            }
            if (StringUtil.isNullOrEmpty(docData.getShengMingTZ().getTiZhiZS())) {
                docData.getShengMingTZ().setTiZhiZS("24.11");
            }
            if (StringUtil.isNullOrEmpty(docData.getShengMingTZ().getQiTaYXTZ())) {
                docData.getShengMingTZ().setQiTaYXTZ("无");
            }
        }

        if (docData.getShengHuoFS() == null) {
            ShengHuoFS shengHuoFS = new ShengHuoFS();
            shengHuoFS.setRiXiYL("10");
            shengHuoFS.setRiYinJL("3");
            shengHuoFS.setDuanLianPL(new CodeObject("2", "每周一次以上"));
            shengHuoFS.setMeiCiDLSJ("30");
            shengHuoFS.setRiZhuSL("350");
            shengHuoFS.setXinLiTZ(new CodeObject("2", "一般"));
            shengHuoFS.setZunYiXW(new CodeObject("2", "一般"));
            docData.setShengHuoFS(shengHuoFS);
        } else {
            if (StringUtil.isNullOrEmpty(docData.getShengHuoFS().getRiXiYL())) {
                docData.getShengHuoFS().setRiXiYL("10");
            }
            if (StringUtil.isNullOrEmpty(docData.getShengHuoFS().getRiYinJL())) {
                docData.getShengHuoFS().setRiYinJL("3");
            }
            if (StringUtil.isNullOrEmpty(docData.getShengHuoFS().getMeiCiDLSJ())) {
                docData.getShengHuoFS().setMeiCiDLSJ("30");
            }
            if (StringUtil.isNullOrEmpty(docData.getShengHuoFS().getRiZhuSL())) {
                docData.getShengHuoFS().setRiZhuSL("350");
            }
            if (docData.getShengHuoFS().getDuanLianPL() == null || StringUtil.isNullOrEmpty(docData.getShengHuoFS().getDuanLianPL().getCode())) {
                docData.getShengHuoFS().setDuanLianPL(new CodeObject("2", "每周一次以上"));
            }
            if (docData.getShengHuoFS().getXinLiTZ() == null || StringUtil.isNullOrEmpty(docData.getShengHuoFS().getXinLiTZ().getCode())) {
                docData.getShengHuoFS().setXinLiTZ(new CodeObject("2", "一般"));
            }
            if (docData.getShengHuoFS().getZunYiXW() == null || StringUtil.isNullOrEmpty(docData.getShengHuoFS().getZunYiXW().getCode())) {
                docData.getShengHuoFS().setZunYiXW(new CodeObject("2", "一般"));
            }
        }

        if (docData.getZhiLiaoJH() == null) {
            ZhiLiaoJH zhiLiaoJH = new ZhiLiaoJH();
            zhiLiaoJH.setMuBiaoTZ("55.0");
            zhiLiaoJH.setMuBiaoTZZS("22.22");
            zhiLiaoJH.setMuBiaoRXYL("1");
            zhiLiaoJH.setMuBiaoRYJL("1");
            zhiLiaoJH.setMuBiaoYDSC("30");
            zhiLiaoJH.setMuBiaoDLPL(new CodeObject("21", "5次/周～6次/周"));
            zhiLiaoJH.setMuBiaoRZSL("300");
            docData.setZhiLiaoJH(zhiLiaoJH);
        } else {
            if (StringUtil.isNullOrEmpty(docData.getZhiLiaoJH().getMuBiaoTZ())) {
                docData.getZhiLiaoJH().setMuBiaoTZ("10");
            }
            if (StringUtil.isNullOrEmpty(docData.getZhiLiaoJH().getMuBiaoTZZS())) {
                docData.getZhiLiaoJH().setMuBiaoTZZS("22.22");
            }
            if (StringUtil.isNullOrEmpty(docData.getZhiLiaoJH().getMuBiaoRXYL())) {
                docData.getZhiLiaoJH().setMuBiaoRXYL("1");
            }
            if (StringUtil.isNullOrEmpty(docData.getZhiLiaoJH().getMuBiaoRYJL())) {
                docData.getZhiLiaoJH().setMuBiaoRYJL("1");
            }
            if (StringUtil.isNullOrEmpty(docData.getZhiLiaoJH().getMuBiaoYDSC())) {
                docData.getZhiLiaoJH().setMuBiaoYDSC("30");
            }
            if (StringUtil.isNullOrEmpty(docData.getZhiLiaoJH().getMuBiaoRZSL())) {
                docData.getZhiLiaoJH().setMuBiaoRZSL("300");
            }
            if (docData.getZhiLiaoJH().getMuBiaoDLPL() == null || StringUtil.isNullOrEmpty(docData.getZhiLiaoJH().getMuBiaoDLPL().getCode())) {
                docData.getZhiLiaoJH().setMuBiaoDLPL(new CodeObject("21", "5次/周～6次/周"));
            }
        }

        if (docData.getShiYanSJC() == null || CollectionUtils.isEmpty(docData.getShiYanSJC().getShiYanSJCXM())) {
            ShiYanSJC shiYanSJC = new ShiYanSJC();
            shiYanSJC.setShiYanSJCXM(new ArrayList<>());
            shiYanSJC.setKongFuXT("8.4");
            shiYanSJC.setTangHuaXHDB("4.0");

            ShiYanSJCXM item = new ShiYanSJCXM();
            item.setJianCeRQ(CDAUtil.defaultD8());
            item.setFuZhuJCXM("糖源检测");
            item.setFuZhuJCRYXM("-");
            item.setFuZhuJCJG("异常");
            shiYanSJC.getShiYanSJCXM().add(item);
            docData.setShiYanSJC(shiYanSJC);
        }

        if (CollectionUtils.isEmpty(docData.getYongYao())) {
            List<YongYao> yongYaoList = new ArrayList<>();
            YongYao yongYao = new YongYao();
            yongYao.setZhongYaoSYLB(new CodeObject("1", "未使用"));
            yongYao.setYiDaoSDCJL(new ValueUnit("1", "支"));
            yongYao.setYiDaoSSYPL(new ValueUnit("1", "次/日"));
            yongYao.setYiDaoSMC("-");
            yongYao.setDiXueTFY(new CodeObject("1", "无"));

            List<YongYaoItem> yongYaoItemList = new ArrayList<>();
            YongYaoItem item = new YongYaoItem();
            item.setYongYaoTJ(new CodeObject("9", "其他用药途径"));
            item.setDanCiYYJL(new ValueUnit("1.00", "mg"));
            item.setYaoWuSYPL(new ValueUnit("1", "次/日"));
            item.setYaoPinMC("二甲双胍片");
            item.setFuYaoYCX(new CodeObject("1", "规律"));
            item.setYongYaoFZY("无");
            item.setYongYaoFZYBZ("false");
            yongYaoItemList.add(item);
            yongYao.setYongYaoItemList(yongYaoItemList);
            yongYaoList.add(yongYao);
            docData.setYongYao(yongYaoList);
        }

        if (docData.getJianKangPG() == null || docData.getJianKangPG().getSuiFangPJ() == null || StringUtil.isNullOrEmpty(docData.getJianKangPG().getSuiFangPJ().getCode())) {
            JianKangPG jianKangPG = new JianKangPG();
            jianKangPG.setSuiFangPJ(new CodeObject("2", "控制不满意"));
            docData.setJianKangPG(jianKangPG);
        }

        if (docData.getZhuanZhenJY() == null) {
            ZhuanZhenJY zhuanZhenJY = new ZhuanZhenJY();
            zhuanZhenJY.setZhuanZhenBZ("false");
            zhuanZhenJY.setZhuanRuJGMC("-");
            zhuanZhenJY.setZhuanRuKSMC("分泌科");
            zhuanZhenJY.setZhuanZhenYY("无");
            docData.setZhuanZhenJY(zhuanZhenJY);
        }

        if (docData.getXiaCiSFAP() == null) {
            XiaCiSFAP xiaCiSFAP = new XiaCiSFAP();
            xiaCiSFAP.setXiaCiSFRQ(CDAUtil.defaultD8());
            docData.setXiaCiSFAP(xiaCiSFAP);
        }
    }
}
